package com.liao.note.controller;

import com.liao.note.dao.ReactionDao;
import com.liao.note.domain.Reaction;
import com.liao.note.domain.User;
import com.liao.note.enums.ResultCode;
import com.liao.note.service.UserService;
import com.liao.note.vo.ResultVO;
import com.liao.note.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (User)表控制层
 *
 * @author long
 * @since 2023-02-11 19:59:12
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;
    @Autowired
    private ReactionDao reactionDao;

    /**
     * 登录
     *
     * @return 用户信息
     */
    @PostMapping("/login")
    public ResultVO<Object> login(@RequestBody User user) {
        String account = user.getAccount();
        String password = user.getPassword();
        UserVO userVO = userService.login(account, password);
        if (userVO == null) {
            return new ResultVO<>(ResultCode.UNEXIST_PASSWORD);
        }
        return new ResultVO<>(userVO);
    }

    /**
     * 注册
     *
     * @return 用户信息
     */
    @PostMapping("/register")
    public ResultVO<Object> register(@RequestBody User user) {
        String account = userService.register(user);
        if (!account.equals("")) {
            return new ResultVO<>(ResultCode.SUCCESS, account);
        } else {
            return new ResultVO<>(ResultCode.REQUEST_ERROR, "注册失败");
        }
    }

    @PostMapping("/updatePwd")
    public ResultVO<Object> updatePwd(@RequestBody User user) {
        if (userService.updatePwd(user)) {
            return new ResultVO<>(ResultCode.SUCCESS, "修改成功");
        } else {
            return new ResultVO<>(ResultCode.SUCCESS, "修改失败");
        }
    }

    @PostMapping("/reaction")
    public ResultVO<Object> reaction(@RequestBody Reaction reaction) {
        reaction.setCreateTime(new Date());
        reactionDao.insert(reaction);
        return new ResultVO<>(ResultCode.SUCCESS, "提交成功");
    }

}
